<?php
namespace app\index\model;
use think\Model;
use think\Config;
use think\Db;
/**
 * 模型类
 * Class User
 * @package app\api\model
 */
class Section extends Model
{


    protected $name = 'section';


    public function getSection($where,$field='*',$order="id desc"){
        if(!$where) return false;
        $listArr = $this->field($field)->where($where)->order($order)->select();
        $listArr = collection($listArr)->toarray();
        return $listArr;
    }

    public function getSectionList($where,$limit){
        if(!$where) return false;
        $count = $this->where($where)->count();

        $field = "*";
        $reData = $this->where($where)->field($field)->limit($limit)->order("id asc")->select();
        $arr = [
            'count'=>$count,
            'list'=>$reData
        ];
        return $arr;
    }

    //分类型获取断面信息
    public function getTypeSection($where,$field='*'){
        if(!$where) return false;
        $listArr = $this->field($field)->where($where)->select();
        $listArr = collection($listArr)->toarray();

        $reArr = [];
        foreach($listArr as $k=>$v){
            $reArr[$v['tasktype_id']][] = $v;
        }

        return $reArr;
    }

    //获取当月未采样断面
    public function getMonthUnSampling($sectionIdArr,$com_id){
        if(!$sectionIdArr || !$com_id) return false;
        $start_time = strtotime(date("Y-m").'-01');
        $end_time = time();

        $where['id'] = ['in',$sectionIdArr];

        $where['_string'] = "(select count(id) from cy_sampling where id=section_id and com_id='{$com_id}' and sample_time between {$start_time} and {$end_time})=0 ";
        $field = "id,posname";
        $arr = $this->where($where)->field($field)->select();


        return $arr;
    }

}
